/**	

MIT License

Copyright (c) 2010, Markus Broecker <mbrckr@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**	Applies a translation to the current matrix.
*/
mat4 translate(const in vec3 t)
{
	return mat4(1.0, 0.0, 0.0, 0.0,
				0.0, 1.0, 0.0, 0.0,
				0.0, 0.0, 1.0, 0.0,
				t.x, t.y, t.z, 1.0);
}

/**	
	Creates a projection matrix, the same way gluPerspective does.
	@param fovy The vertical field of view angle in degrees
	@param aspect The horizontal to vertical aspect ratio
	@param near Nearclip plane distance
	@param far Farclip plane distance
*/	
mat4 perspective(float fovy, float aspect, float near, float far)
{
	float f = 1.0 / tan(radians(fovy)/2.0);	
	float quot = 1.0 / (near-far);
	
	return mat4(f/aspect, 0.0, 0.0, 0.0, 
				0.0, f, 0.0, 0.0,
				0.0, 0.0, (far+near)*quot, -1.0,
				0.0, 0.0, (2.0*far*near)*quot, 0.0);
}

/**
	Creates a look-at matrix, similar to gluLookAt.
	@param eye Eye position
	@param at Target position
	@param up Helper-Up Vector
*/
mat4 lookat(const in vec3 eye, const in vec3 at, const in vec3 up)
{	
	vec3 z = normalize(eye - at);
	vec3 x = normalize(cross(up, z));
	vec3 y = cross(z, x);
	
	mat4 m = mat4(	x.x, y.x, z.x, 0.0,
					x.y, y.y, z.y, 0.0,
					x.z, y.z, z.z, 0.0,
					0.0, 0.0, 0.0, 1.0);
	return m * translate(-eye);
}

struct Camera
{
	vec3 position, at, up;
	float fov, ratio, near, far;
};
uniform Camera	camera;

mat4 ModelMatrix = gl_ModelViewMatrix;

varying vec3 position, normal;
varying vec3 color;

void main()
{
	mat4 ProjMatrix = perspective(camera.fov, camera.ratio, camera.near, camera.far);
	mat4 ViewMatrix = lookat(camera.position, camera.at, camera.up);
	
	gl_Position = ProjMatrix * ViewMatrix * ModelMatrix * gl_Vertex;
	
	
	/* position and normal in world coordinates */
	position = (ModelMatrix * gl_Vertex).xyz;
	/* This only works for uniform scales! */
	normal = (ModelMatrix * vec4(gl_Normal, 0.0)).xyz;  
	
	color = gl_Color.rgb;
	
}